'use strict';

import Base from './base.js';

export default class extends Base {
    /**
     * index action
     * @return {Promise} []
     */

    init(http) {
        super.init(http);
        let weichatService = think.service('weichat', 'weichat')
        this.weichatService = new weichatService();
    }


    async indexAction() {
        let self = this;
        var redirectURI = 'http://h.fulihuodong.com:8888/api/codehandler';
        let tyhd = self.config('sh00ok');
        await self.session('callBackUrl', self.get('callBackUrl'));
        let isBeUserId = self.get('isBeUserId');
        return this.redirect(tyhd.url + tyhd.APPID + '&scope=snsapi_userinfo&state=1&redirect_uri=' + redirectURI + '?isBeUserId=' + isBeUserId);
        // console.log(tyhd.url+tyhd.APPID+'&scope=snsapi_userinfo&state=1&redirect_uri='+ redirectURI)

    }

    async codehandlerAction() {
        let self = this;
        let code = this.get('code');
        let config = this.config('sh00ok');
        config['code'] = code;
        let token = await self.weichatService.getToken(config).then(function(data) {
            return data;
        }).catch(function(err) {
            console.log(err);
        });
        let user = await self.weichatService.getUserInfo(JSON.parse(token)).then(function(data) {
            return data;
        }).catch(function(err) {
            console.log(err);
        });
        let userId = await self.weichatService.addUser(JSON.parse(user));
        let callBackUrl = await this.session('callBackUrl');
        let isBeUserId = self.get('isBeUserId');
        console.log('isBeUserId')
        console.log(isBeUserId)
        if (!think.isEmpty(isBeUserId)) {
            var reids = this.service('redisservice');
            self.redis = new reids();
            console.log('通过分享进入');
            console.log(userId)
            if (isBeUserId != userId) {
                console.log('新用户');
                await self.redis.addWechatUserInfo(isBeUserId, user);
                await self.redis.updata('allView' + isBeUserId);
                await self.redis.updata('allViewDay' + isBeUserId);
                let D = new Date()
                let date = new Date(D.getFullYear() + "-" + (D.getMonth() + 1) + "-" + D.getDate() + "-23:59:59");
                let timeStamp = date.getTime();
                let indexTimeStamp = (new Date()).getTime();
                let clearTime = ((timeStamp - indexTimeStamp) / 1000);
                console.log(clearTime + "秒后清除allViewDay" + isBeUserId + "标识")
                await this.redis.setClearTime('allViewDay' + isBeUserId, Math.ceil(clearTime));
            }
        }
        let _userParse = JSON.parse(user);
        let _data={
            openid:_userParse.openid,
            userid:userId,
            headImg:_userParse.headimgurl,
            name:encodeURIComponent(_userParse.nickname)
         }
        this.redirect(callBackUrl + '?data='+encodeURIComponent(JSON.stringify(_data)));
    }

    async viewhistoryAction() {
        let self = this;
        let id = self.get('userid')
        let user = await this.weichatService.getWechatViewHistory(id);
        let addViewAll=await this.weichatService.getCount('allView'+id);
        let addViewDay=await this.weichatService.getCount('allViewDay'+id);
        let userModel={
            user:user,
            viewAll:addViewAll,
            viewDay:addViewDay
        }
        console.log(userModel)
        return self.success(userModel)
    }
}
